package com.ebupt.migu.music.effectStatistics.controller;

import com.ebupt.migu.music.common.auth.IDAuthUtils;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.util.ExcelUtils;
import com.ebupt.migu.music.common.util.ReqestParamUtil;
import com.ebupt.migu.music.common.util.excelSheet.Sheet;
import com.ebupt.migu.music.effectStatistics.service.HeavyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/heavyUsers")
public class HeavyUsersController {

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private HttpServletResponse response;
    @Autowired
    private HeavyUserService HeavyUserService;
    @Autowired
    private IDAuthUtils idAuthUtils;

    private static String[] columnsArr = {"startTime","createTime", "strategyId", "strategyName", "executeType","groupName", "userGroupSize",
            "preSendTime", "preActiveUserGroupSize","prePlatinumUserGroupSize", "preAuditionTimes","preAuditionDuration",
            "sendTime","activeUserGroupSize","platinumUserGroupSize","auditionTimes","auditionDuration"};

    private static String[] headArr = {"策略执行时间","策略创建时间","策略ID", "策略名称", "执行类型", "目标人群", "人群规模",
            "策略下发前日期区间","策略下发前日活用户数","策略下发前白金会员用户数", "策略下发前试听次数", "策略下发前试听时长",
            "策略下发后日期区间", "策略下发后日活用户数", "策略下发后白金会员用户数","策略下发后试听次数","策略下发后试听时长"};

    /**
     * 分页、条件查询
     * 查询条件： 开始时间、结束时间、策略名称
     *
     * @return
     */
    @PostMapping("/queryMap")
    public ResultObject queryMap() {
        Map<String, Object> params = ReqestParamUtil.getParamsWithUserInfo(request);
        //params = idAuthUtils.isAdmin(params);
        return HeavyUserService.queryMap(params);
    }

    /**
     *  效果统计消息通知 -- 目标人群条件查询
     *
     * @return
     */
    @PostMapping("/targetUser")
    public ResultObject targetUser() {
        Map<String, Object> params = ReqestParamUtil.getParamsWithUserInfo(request);
        //params = idAuthUtils.isAdmin(params);
        return HeavyUserService.targetUser(params);
    }

    /**
     * 查询条件： 创建时间、执行时间、策略名称、执行类型、目标人群
     * 全量导出
     *
     * @return
     */
    @RequestMapping(value = "/exportMap", produces = MediaType.TEXT_HTML_VALUE)
    public void exportMap() {
        Map<String, Object> params = ReqestParamUtil.getParamsWithUserInfo(request);
        //params = idAuthUtils.isAdmin(params);
        System.out.println("重度用户输入参数");
        System.out.println(params);
        List<HashMap<String, Object>> list = HeavyUserService.exportMap(params);
        ExcelUtils excel = new ExcelUtils();
        excel.setName("heavy_users").setVersion("2007");//excel 2007's name
        Sheet sheet = new Sheet();
        System.out.println("重度用户导出数据");
        System.out.println(list);
        sheet.setName("重要用户");//sheet's name
        sheet.setHead(headArr);
        sheet.fillData2(list, columnsArr);
        excel.addSheet(sheet);
        excel.zipAndResponse(response, request);
    }
}
